package com.yuncheng.oaApi.controller;

import java.io.*;
import java.net.URLEncoder;

import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.*;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONObject;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.NkRsnd;
import com.yuncheng.oaApi.service.INkRsNdService;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.I18nUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.NkConstant;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.NkRsYszbczjlMapper;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.NkRsYszbczjl;
import com.yuncheng.oaApi.service.INkRsYszbczjlService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.vo.NkYsStatVo;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.springframework.context.annotation.Lazy;



import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import utils.DateConversionUtils;


/**
 * @Description: 内控-预算指标操作记录
 * @Author: lhb
 * @Date: 2023-11-20
 * @Version: V1.0
 */
@Api(tags = "NK_内控-预算指标操作记录接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsYszbczjl")
public class NkRsYszbczjlController extends BaseController<NkRsYszbczjl, INkRsYszbczjlService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsYszbczjlController.class);

    @Resource
    private INkRsYszbczjlService nkRsYszbczjlService;

    @Resource
    @Lazy
    private NkRsYszbczjlMapper nkRsYszbczjlMapper;

    @Resource
    @Lazy
    private MyExcelUtils myExclUtil;


    @Resource
    private OaBusinessConstant oaBusinessConstant;


    @Resource
    @Lazy
    private INkRsNdService nkRsNdService;


    /**
     * 分页列表查询
     *
     * @param nkRsYszbczjl
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-分页列表查询")
    @ApiOperation(value = "内控-预算指标操作记录-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsYszbczjl nkRsYszbczjl,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(name = "startTime", required = false) String startTime,
                                       @RequestParam(name = "endTime", required = false) String endTime) throws ParseException {

        List<NkRsnd> nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                .eq(NkRsnd::getFsfxz, OaCommonFlowConstant.NDXZ_YES));


        LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();

        if (CollectionUtils.isNotEmpty(nkRsnds)){
            queryWrapper.eq(NkRsYszbczjl :: getFnd,nkRsnds.get(0).getFnd());
        }else {
            queryWrapper.eq(NkRsYszbczjl :: getFnd,DateConversionUtils.DateToYear(new Date()));
        }

        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbxr()), NkRsYszbczjl::getFbxr, nkRsYszbczjl.getFbxr());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbxrbm()), NkRsYszbczjl::getFbxrbm, nkRsYszbczjl.getFbxrbm());
        queryWrapper.like(NkRsYszbczjl::getFczfs, NkConstant.CZFS_BX);
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFszlx()), NkRsYszbczjl::getFszlx, nkRsYszbczjl.getFszlx());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, nkRsYszbczjl.getFbmzcjjfl());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFystzmc()), NkRsYszbczjl::getFystzmc, nkRsYszbczjl.getFystzmc());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjbm()), NkRsYszbczjl::getFbmzcjjbm, nkRsYszbczjl.getFbmzcjjbm());
//        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFyszbid()),NkRsYszbczjl::getFyszbid, nkRsYszbczjl.getFyszbid());

        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // 不区分大小写
                .appendPattern("yyyy年M月d日 H时m分s秒")
                .toFormatter(Locale.CHINA);

        LocalDateTime startTimes;
        LocalDateTime endTimes;

        if (StringUtils.isNotBlank(startTime)) {
            startTimes = LocalDateTime.parse(startTime, formatter);
        } else {
            startTimes = null;
        }

        if (StringUtils.isNotBlank(endTime)) {
            endTimes = LocalDateTime.parse(endTime, formatter);
        } else {
            endTimes = null;
        }

        if (startTimes != null && endTimes != null) {
            queryWrapper.between(NkRsYszbczjl::getFczsj, startTimes, endTimes);
        }

        queryWrapper.orderByDesc(NkRsYszbczjl::getFczsj);
        Page<NkRsYszbczjl> page = new Page<>(pageNo, pageSize);
        IPage<NkRsYszbczjl> pageList = nkRsYszbczjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    /**
     * 分页列表查询
     *
     * @param nkRsYszbczjl
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-审计列表查询")
    @ApiOperation(value = "内控-预算指标操作记录-审计列表查询")
    @GetMapping(value = "/queryPageSj")
    public HttpResult<?> queryPageSj(NkRsYszbczjl nkRsYszbczjl,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(name = "startTime", required = false) String startTime,
                                     @RequestParam(name = "endTime", required = false) String endTime) throws ParseException {

        List<NkRsnd> nkRsnds = nkRsNdService.list(new LambdaQueryWrapper<NkRsnd>()
                .eq(NkRsnd::getFsfxz, OaCommonFlowConstant.NDXZ_YES));



        LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isNotEmpty(nkRsnds)){
            String fnd = nkRsnds.get(0).getFnd();
            queryWrapper.eq(NkRsYszbczjl :: getFnd,fnd);
        }else {
            queryWrapper.eq(NkRsYszbczjl :: getFnd, DateConversionUtils.DateToYear(new Date()));
        }


        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFczr()), NkRsYszbczjl::getFczr, nkRsYszbczjl.getFczr());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFczbm()), NkRsYszbczjl::getFczbm, nkRsYszbczjl.getFczbm());
        queryWrapper.eq(NkRsYszbczjl::getFczfs, NkConstant.CZFS_CSHDR)
                .or()
                .eq(NkRsYszbczjl::getFczfs, NkConstant.CZFS_SQBR)
                .or()
                .eq(NkRsYszbczjl::getFczfs, NkConstant.CZFS_SQBC);
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFszlx()), NkRsYszbczjl::getFszlx, nkRsYszbczjl.getFszlx());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, nkRsYszbczjl.getFbmzcjjfl());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFystzmc()), NkRsYszbczjl::getFystzmc, nkRsYszbczjl.getFystzmc());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjbm()), NkRsYszbczjl::getFbmzcjjbm, nkRsYszbczjl.getFbmzcjjbm());




        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // 不区分大小写
                .appendPattern("yyyy年M月d日 H时m分s秒")
                .toFormatter(Locale.CHINA);

        LocalDateTime startTimes;
        LocalDateTime endTimes;

        if (StringUtils.isNotBlank(startTime)) {
            startTimes = LocalDateTime.parse(startTime, formatter);
        } else {
            startTimes = null;
        }

        if (StringUtils.isNotBlank(endTime)) {
            endTimes = LocalDateTime.parse(endTime, formatter);
        } else {
            endTimes = null;
        }

        if (startTimes != null && endTimes != null) {
            queryWrapper.between(NkRsYszbczjl::getFczsj, startTimes, endTimes);
        }

        queryWrapper.orderByDesc(NkRsYszbczjl::getFczsj);
        Page<NkRsYszbczjl> page = new Page<>(pageNo, pageSize);
        IPage<NkRsYszbczjl> pageList = nkRsYszbczjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    /**
     * 个税（劳务报酬）-分页列表查询
     *
     * @param nkRsYszbczjl
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "内控-个税（劳务报酬）-分页列表查询")
    @ApiOperation(value = "内控-个税（劳务报酬）-分页列表查询")
    @GetMapping(value = "/gsList")
    public HttpResult<?> queryPagegsList(NkRsYszbczjl nkRsYszbczjl,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(name = "startTime", required = false) String startTime,
                                         @RequestParam(name = "endTime", required = false) String endTime) throws ParseException {

        LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbxr()), NkRsYszbczjl::getFbxr, nkRsYszbczjl.getFbxr());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbxrbm()), NkRsYszbczjl::getFbxrbm, nkRsYszbczjl.getFbxrbm());

        queryWrapper.like(NkRsYszbczjl::getFczfs, NkConstant.CZFS_BX);
        queryWrapper.like(NkRsYszbczjl::getFbmzcjjfl, NkConstant.BMZCJJFLMC_LWF);
        queryWrapper.like(NkRsYszbczjl::getFszlx, NkConstant.SZLX_ZC);
        queryWrapper.gt(NkRsYszbczjl::getFczje, NkConstant.ZDFY);


        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, nkRsYszbczjl.getFbmzcjjfl());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFystzmc()), NkRsYszbczjl::getFystzmc, nkRsYszbczjl.getFystzmc());
        queryWrapper.like(StringUtils.isNotBlank(nkRsYszbczjl.getFbmzcjjbm()), NkRsYszbczjl::getFbmzcjjbm, nkRsYszbczjl.getFbmzcjjbm());
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive() // 不区分大小写
                .appendPattern("yyyy年M月d日 H时m分s秒")
                .toFormatter(Locale.CHINA);

        LocalDateTime startTimes;
        LocalDateTime endTimes;

        if (StringUtils.isNotBlank(startTime)) {
            startTimes = LocalDateTime.parse(startTime, formatter);
        } else {
            startTimes = null;
        }

        if (StringUtils.isNotBlank(endTime)) {
            endTimes = LocalDateTime.parse(endTime, formatter);
        } else {
            endTimes = null;
        }

        if (startTimes != null && endTimes != null) {
            queryWrapper.between(NkRsYszbczjl::getFczsj, startTimes, endTimes);
        }

        queryWrapper.orderByDesc(NkRsYszbczjl::getFczsj);
        Page<NkRsYszbczjl> page = new Page<>(pageNo, pageSize);
        IPage<NkRsYszbczjl> pageList = nkRsYszbczjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    @AutoLog(value = "预算统计")
    @ApiOperation(value = "预算统计")
    @GetMapping(value = "queryYsStatPageList")
    public HttpResult queryYsStatPageList(NkYsStatVo query,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<NkYsStatVo> page = new Page<>(pageNo, pageSize);
        Page<NkYsStatVo> pageList = nkRsYszbczjlMapper.findYsStatPageList(page, query);

        return HttpResult.ok(pageList);
    }

    /**
     * 添加
     *
     * @param nkRsYszbczjl
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-添加")
    @ApiOperation(value = "内控-预算指标操作记录-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsYszbczjl nkRsYszbczjl) {
        HttpResult<String> result = new HttpResult(true);
        nkRsYszbczjlService.save(nkRsYszbczjl);
        result.setResult(nkRsYszbczjl.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param nkRsYszbczjl
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-编辑")
    @ApiOperation(value = "内控-预算指标操作记录-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsYszbczjl nkRsYszbczjl) {
        nkRsYszbczjlService.updateById(nkRsYszbczjl);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-通过id删除")
    @ApiOperation(value = "内控-预算指标操作记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsYszbczjlService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-批量删除")
    @ApiOperation(value = "内控-预算指标操作记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsYszbczjlService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "内控-预算指标操作记录-通过id查询")
    @ApiOperation(value = "内控-预算指标操作记录-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsYszbczjl nkRsYszbczjl = nkRsYszbczjlService.getById(id);
        return HttpResult.ok(nkRsYszbczjl);
    }


    @AutoLog(value = "内控-根据选择导出审计记录")
    @ApiOperation(value = "内控-导出")
    @GetMapping(value = "/exportYszbczjlData")
    public void exportYszbczjlData(@RequestParam(value = "ids", defaultValue = "") String ids,
                                   NkRsYszbczjl ndczjlBasic,
                                   HttpServletResponse response) {
        try {
            String name = "预算指标审计记录";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name + ".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出 如果id为空，则根据传入的数据查询导出，如果不为空，则根据id查询导出
            if (StringUtils.isNotEmpty(ids)) {
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            } else {
                LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();
                //操作人
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczr()), NkRsYszbczjl::getFczr, ndczjlBasic.getFczr());
                //部门支出经济分类
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, ndczjlBasic.getFbmzcjjfl());
                //预算项目
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFyszbmc()), NkRsYszbczjl::getFyszbmc, ndczjlBasic.getFyszbmc());
                //科室
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczbm()), NkRsYszbczjl::getFczbm, ndczjlBasic.getFczbm());
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            }
            myExclUtil.outputListToExclFile(OaBusinessConstant.INSPECTOR_NK_NKRSYSZBCZJL_XML, list, out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出项目预算指标操作数据出错-{}", e.getMessage());
        }
    }


    @AutoLog(value = "内控-根据选择导出操作记录")
    @ApiOperation(value = "内控-导出")
    @GetMapping(value = "/exportYszbczjl")
    public void exportYszbczjl(@RequestParam(value = "ids", defaultValue = "") String ids,
                               NkRsYszbczjl ndczjlBasic,
                               HttpServletResponse response) {
        try {
            String name = "预算指标操作记录";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name + ".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出 如果id为空，则根据传入的数据查询导出，如果不为空，则根据id查询导出
            if (StringUtils.isNotEmpty(ids)) {
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            } else {
                LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();
                //操作人
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczr()), NkRsYszbczjl::getFczr, ndczjlBasic.getFczr());
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbxr()), NkRsYszbczjl::getFbxr, ndczjlBasic.getFbxr());
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbxrbm()), NkRsYszbczjl::getFbxrbm, ndczjlBasic.getFbxrbm());
                //部门支出经济分类
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, ndczjlBasic.getFbmzcjjfl());
                //预算项目
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFyszbmc()), NkRsYszbczjl::getFyszbmc, ndczjlBasic.getFyszbmc());
                //科室
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczbm()), NkRsYszbczjl::getFczbm, ndczjlBasic.getFczbm());
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            }
            myExclUtil.outputListToExclFile(OaBusinessConstant.INSPECTOR_NK_YSZBCZJL_XML, list, out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出项目预算指标操作数据出错-{}", e.getMessage());
        }
    }


    @AutoLog(value = "内控-导出个税（劳务报酬）申报明细")
    @ApiOperation(value = "内控-导出个税（劳务报酬）申报明细")
    @GetMapping(value = "/exportgsYszbczjl")
    public void exportgsYszbczjl(@RequestParam(value = "ids", defaultValue = "") String ids,
                                 NkRsYszbczjl ndczjlBasic,
                                 HttpServletResponse response) {
        try {
            String name = "预算指标操作记录";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name + ".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出 如果id为空，则根据传入的数据查询导出，如果不为空，则根据id查询导出
            if (StringUtils.isNotEmpty(ids)) {
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            } else {
                LambdaQueryWrapper<NkRsYszbczjl> queryWrapper = new LambdaQueryWrapper<>();
                //操作人
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczr()), NkRsYszbczjl::getFczr, ndczjlBasic.getFczr());
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbxr()), NkRsYszbczjl::getFbxr, ndczjlBasic.getFbxr());
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbxrbm()), NkRsYszbczjl::getFbxrbm, ndczjlBasic.getFbxrbm());
                //部门支出经济分类
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFbmzcjjfl()), NkRsYszbczjl::getFbmzcjjfl, ndczjlBasic.getFbmzcjjfl());
                //预算项目
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFyszbmc()), NkRsYszbczjl::getFyszbmc, ndczjlBasic.getFyszbmc());
                //科室
                queryWrapper.like(StringUtils.isNotBlank(ndczjlBasic.getFczbm()), NkRsYszbczjl::getFczbm, ndczjlBasic.getFczbm());
                List<NkRsYszbczjl> ndkpBasicList = nkRsYszbczjlService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)) {
                    for (NkRsYszbczjl e : ndkpBasicList) {
                        list.add(e);
                    }
                }
            }
            myExclUtil.outputListToExclFile(OaBusinessConstant.INSPECTOR_NK_GSCZJL_XML, list, out); // 导出模板数据成excel


        } catch (Exception e) {
            log.error("导出项目预算指标操作数据出错-{}", e.getMessage());
        }
    }


    /**
     * 新的导出 表格
     */



//        @PostMapping(value = "ExportYszb")
        @GetMapping(value = "ExportYszb")
        public void ExportYszb(String ids,HttpServletResponse response) throws IOException {

            String uploadFileOafj = oaBusinessConstant.UPLOAD_FILE_OAFJ;

            String templatePath = uploadFileOafj + "/" +"lwbc.xls"; //模板文件的地址
            String filePath = uploadFileOafj +"/"+"file.txt";

            if(StringUtils.isBlank(ids)){
                throw new BusinessException("没有选择导出的数据");
            }


            List<NkRsYszbczjl> nkRsYszbczjls = nkRsYszbczjlService.listByIds(Arrays.asList(ids.split(",|，")));
            List<NkRsYszbczjl> data = nkRsYszbczjls; // 数据库查出来数据

            Integer dataStartRow = 1;   //开始填充数据的区域行号下标。从0开始数


            JSONObject fileConfig = readJsonFromFile(filePath);
            //调用工具类
            nkRsYszbczjlService.exportUseExcelTemplate(templatePath,data,dataStartRow,fileConfig,response);
        }


    /*public static JSONObject readJsonFromFile(String filePath) throws IOException {
        StringBuilder fileData = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));

        char[] buf = new char[1024];
        int numRead = 0;
        while((numRead = reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();

        return JSONObject.parseObject(fileData.toString());
    }*/

    public static JSONObject readJsonFromFile(String filePath) throws IOException {
        StringBuilder fileData = new StringBuilder();
        try (InputStreamReader reader = new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8)) {
            char[] buf = new char[1024];
            int numRead;
            while ((numRead = reader.read(buf)) != -1) {
                fileData.append(buf, 0, numRead);
            }
        }

        return JSONObject.parseObject(fileData.toString());
    }


}
